/*
 * Copyright (C) 2022 Huawei Technologies Co., Ltd.
 * Licensed under the Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *     http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
 * PURPOSE.
 * See the Mulan PSL v2 for more details.
 */

#ifndef TEED_TEE_H
#define TEED_TEE_H

/*
 * SMC function IDs that TEE uses to signal various forms of completions
 * to the secure payload dispatcher.
 */
#define TEE_ENTRY_DONE            0xf2000000
#define TEE_ON_DONE               0xf2000001
#define TEE_OFF_DONE              0xf2000002
#define TEE_SUSPEND_DONE          0xf2000003
#define TEE_RESUME_DONE           0xf2000004
#define TEE_PREEMPTED             0xf2000005
#define TEE_ABORT_DONE            0xf2000007
#define TEE_SYSTEM_OFF_DONE       0xf2000008
#define TEE_SYSTEM_RESET_DONE     0xf2000009

/*
 * Function identifiers to handle S-EL1 interrupt through the synchronous
 * handling model. If the TEE was previously interrupted then control has to
 * be returned to the TEED after handling the interrupt else execution can
 * remain in the TEE.
 */
#define TEE_HANDLED_S_EL1_INTR    0xf2000006

/* SMC function ID that TEE uses to request service from secure monitor */
#define TEE_GET_ARGS              0xf2001000

/*
 * Identifiers for various TEE services. Corresponding function IDs (whether
 * fast or yielding) are generated by macros defined below
 */
#define TEE_ADD                              0x2000
#define TEE_SUB                              0x2001
#define TEE_MUL                              0x2002
#define TEE_DIV                              0x2003
#define TEE_HANDLE_SEL1_INTR_AND_RETURN      0x2004

/*
 * Identify a TEE service from function ID filtering the last 16 bits from the
 * SMC function ID
 */
#define TEE_BARE_FID(fid)  ((fid) & 0xffffu)

/*
 * Generate function IDs for TEE services to be used in SMC calls, by
 * appropriately setting bit 31 to differentiate yielding and fast SMC calls
 */
#define TEE_YIELD_FID(fid) ((TEE_BARE_FID(fid) | 0x72000000u))
#define TEE_FAST_FID(fid)  ((TEE_BARE_FID(fid) | 0x72000000u) | (1u << 31))

/* SMC function ID to request a previously preempted yielding smc */
#define TEE_FID_RESUME     TEE_YIELD_FID(0x3000u)
/*
 * SMC function ID to request abortion of a previously preempted yielding SMC. A
 * fast SMC is used so that the TEE abort handler does not have to be
 * reentrant.
 */
#define TEE_FID_ABORT       TEE_FAST_FID(0x3001u)

/*
 * Total number of function IDs implemented for services offered to NS clients.
 * The function IDs are defined above
 */
#define TEE_NUM_FID         0x5

/* TEE implementation version numbers */
#define TEE_VERSION_MAJOR   0x0 /* Major version */
#define TEE_VERSION_MINOR   0x1 /* Minor version */

#endif /* TEE_H */
